<import resource="classpath:alfresco/webscripts/org/alfresco/confluence/util/utils.js">

// Connect to remote Alfresco webscript service.

// Prepare a remote connector to Alfreso service.
var alfWSServiceUrl = args.url;

var alfEndpoint = CMISUtils.endpoint.getEndpoint();

var connector = remote.connect(alfEndpoint);

var s = new Object();

try {

	// Invoke the remote service.
	var queryResult = connector.call(stringUtils.urlEncodeComponent(alfWSServiceUrl));

	// Check the response status code.
	if (queryResult.status == 200) {

	   var headers =  queryResult.status.headers;
	   
	   var responseType = headers["Content-Type"]
	   
	   if ( responseType.indexOf("application/atom+xml;type=feed") != -1 ) {
	   
	   	// Generate the feed object for the view template.
	   	
	   	s.nodes = atom.toFeed(queryResult.response);
	   
	   } else if (responseType.indexOf("application/json;") != -1) {  
	   
	   	// Generate the JSON object for the view template
	   	
	   	s.json = eval("("+(queryResult.response)+")");
	   
	   } else {
	   
	   	// Return a string for the view template.
	   	
	   	s.response = queryResult.response;
	   
	   }
	   
	} else {

	   s.error = queryResult.response;

	}


} catch (e) {
 
 	s.error = "ERROR: " + e + "<br/><br/>";

    if (e.rhinoException != null) {
      
        var re = e.rhinoException;
           //model.iftlresult += "Stack Trace: <br/><br/>";

         var stackTrace = re.getStackTrace() ;
         var out2 = " ";
         for each (n in stackTrace) {         
           out2 += n + "<br>&nbsp;&nbsp;&nbsp;";
        }
           s.error += out2 + "<br/>";
    }

}

model.s = s;

model.endpoint = alfEndpoint;
  
model.endpointurl = connector.getDescriptor().getEndpointUrl();